﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using TPOJ.Web.ViewModel;
using System.Dynamic;
using TPOJ.BLL;
using TPOJ.Model;

namespace TPOJ.Web.Controllers
{
    public class ContestController : Controller
    {
        //
        // GET: /Contest/

        public ActionResult Index()
        {
            ViewData.Model = ContestDataProxy.GetContests();
            return View();
        }

        public ActionResult Details(int id)
        {
            if (User.Identity.IsAuthenticated)
            {
                AccountBO accountBO = new AccountBO();
                int userId = accountBO.GetAccountByName(User.Identity.Name).UniqueId;
                ViewData["TriedIds"] = SolutionDataProxy.GetTriedNotACedProblemIdsByUserInContest(userId, id);
                ViewData["SolvedIds"] = SolutionDataProxy.GetSolvedProblemIdsByUserInContest(userId, id);
            }
            ViewData["current"] = DateTime.Now.ToString();
            ViewData.Model = ContestDataProxy.GetContest(id);
            return View();
        }

        public ActionResult Standing(int id)
        {
            var contest = ContestDataProxy.GetContest(id);
            ViewData["length"] = contest.Problems.Count();
            ViewData["contestTitle"] = contest.Title;            
            ViewData.Model = ContestDataProxy.GetContestStanding(id);
            return View();
        }

        public ActionResult Statistics(int id)
        {
            var contest = ContestDataProxy.GetContest(id);
            ViewData["contestTitle"] = contest.Title;

            //Do statistics
            var solutions = SolutionDataProxy.GetContestSolutions(id);
            var query = from s in solutions
                        group s by s.Problem into g
                        orderby g.Key.UniqueID
                        select g;

            List<StatisticsRecord> records = new List<StatisticsRecord>();

            foreach (var item in query)
            {
                StatisticsRecord record = new StatisticsRecord((IList<Core.Language>)this.HttpContext.Application["languages"]);

                record.ProblemId = item.Key.UniqueID;
                record.ContestId = id;

                var sols = item.GroupBy(s => s.Result);
                
                foreach (var sol in sols)
                {
                    byte result = sol.Key;
                    record.ResultsCount[result] = sol.Count();
                }

                var solsByLang = item.GroupBy(s => s.Language);

                foreach (var sol in solsByLang)
                {
                    string lang = sol.Key;
                    record.LanguagesCount[lang] = sol.Count();
                }
                
                records.Add(record);
            }
            ViewData.Model = records;
            return View();
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            var contest = new Contest();
            contest.StartTime = DateTime.Now;
            contest.EndTime = DateTime.Now;
            ViewData.Model = contest;

            return View();
        }

        [Authorize(Roles = "Administrators")]
        [HttpPost]
        public ActionResult Create(Contest contest)
        {
            ContestBO contestBO = new ContestBO();
            contestBO.Create(contest);
            return RedirectToAction("AddContestProblem", new { id = contest.UniqueID });
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult AddContestProblem(int id)
        {
            ViewData.Model = ContestDataProxy.GetContest(id);
            return View();
        }

        [Authorize(Roles = "Administrators")]
        [HttpPost]
        public ActionResult AddContestProblem(int UniqueId/* contest id*/, int pid)
        {
            ContestBO contestBO = new ContestBO();
            contestBO.AddContestProblem(UniqueId, pid);
            return RedirectToAction("AddContestProblem", new { id = UniqueId });
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult DeleteContestProblem(int cid, int pid)
        {
            ContestBO contestBO = new ContestBO();
            contestBO.DeleteContestProblem(cid, pid);
            return RedirectToAction("AddContestProblem", new { id = cid });
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id)
        {
            ViewData.Model = ContestDataProxy.GetContest(id);
            return View();
        }

        [Authorize(Roles = "Administrators")]
        [HttpPost]
        public ActionResult Edit(Contest contest)
        {
            ContestBO contestBO = new ContestBO();
            contestBO.Edit(contest);
            return RedirectToAction("AddContestProblem", new { id = contest.UniqueID });
        }
    }
}
